package com.example.schoolenterprisealliance;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import retrofit2.converter.gson.GsonConverterFactory;

import android.content.Context;
import android.content.Intent;
import android.database.Observable;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.activity.DynamicActivity;
import com.example.schoolenterprisealliance.activity.IntroduceActivity;
import com.example.schoolenterprisealliance.activity.ItemActivity;
import com.example.schoolenterprisealliance.activity.SearchHotActivity;
import com.example.schoolenterprisealliance.adapter.Hister_enventAdapter;
import com.example.schoolenterprisealliance.adapter.InformationAdapter;
import com.example.schoolenterprisealliance.adapter.NavigationAdapter;
import com.example.schoolenterprisealliance.adapter.New_InformationAdapter;
import com.example.schoolenterprisealliance.bean.History_event;
import com.example.schoolenterprisealliance.bean.Information;
import com.example.schoolenterprisealliance.bean.Navigation;
import com.example.schoolenterprisealliance.dialog.MyDialog;
import com.example.schoolenterprisealliance.html.HtmlText;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.io.GlideImageLoader;
import com.example.schoolenterprisealliance.method.Skip;
import com.example.schoolenterprisealliance.service.MainService;
import com.example.schoolenterprisealliance.time.Time;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MainActivity extends AppCompatActivity {

    private InformationAdapter informationAdapter;
    private Hister_enventAdapter hister_enventAdapter;

    private RecyclerView IrecyclerView;//资讯表列表
    private RecyclerView HrecycleView;//历史事件表
    private TextView tv_more1;
    private Banner banner;
    private RecyclerView rv_navigation;
    private NavigationAdapter navigationAdapter;
    private Skip skip;//跳转
    private Intent intent;
    private EditText et_search;
    private ImageView iv_search;
    private String string;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IrecyclerView=findViewById(R.id.rv_information);
        HrecycleView=findViewById(R.id.rv_history_event);
        rv_navigation=findViewById(R.id.rv_navigation);
        et_search = findViewById(R.id.et_search);
        iv_search = findViewById(R.id.iv_search);
        tv_more1=findViewById(R.id.tv_more1);
        //一个是前一个activity的退出，另一个activity的进入。
        overridePendingTransition(R.animator.into_right, R.animator.out_left);


        tv_more1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, DynamicActivity.class);
                tv_more1.setTextColor(Color.parseColor("#FF0000"));
                startActivity(intent);
            }
        });
        //设置布局管理器
        LinearLayoutManager linearLayoutManager= new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        IrecyclerView.setLayoutManager(linearLayoutManager);
        IrecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        LinearLayoutManager linearLayoutManager1=new LinearLayoutManager(this);
        linearLayoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        HrecycleView.setLayoutManager(linearLayoutManager1);
        HrecycleView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        LinearLayoutManager linearLayoutManager2 = new GridLayoutManager(this, 5);
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        rv_navigation.setLayoutManager(linearLayoutManager2);
        navigationAdapter=new NavigationAdapter(R.layout.item_navigation, addNavigation());
        rv_navigation.setAdapter(navigationAdapter);

        setBanner();
        informationData();
        historyData();
        navigationListener();
        searchIntent();
        ivImageIntent();
    }
    //轮播图
    private void setBanner(){
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        MainService mainService = retrofit.create(MainService.class);
        //6, 得到Call 的方法
        Call<Information[]> call = mainService.getInformationData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<Information[]>() {
            @Override
            public void onResponse(Call<Information[]> call, Response<Information[]> response) {
                //获得数据
                Information[] information = response.body();
                List<String> imageUrlList=new ArrayList<>();
                for (int i = 0; i < information.length; i++) {
                    imageUrlList.add(information[i].getImgUrl());
                    Log.e("information",information[i].getImgUrl());
                }
                banner = (Banner) findViewById(R.id.banner);
                //设置图片集合
                banner.setImages(imageUrlList);
                //设置banner样式
                banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                //设置图片加载器
                banner.setImageLoader(new GlideImageLoader());
                //设置banner动画效果
                banner.setBannerAnimation(Transformer.DepthPage);
                //设置自动轮播，默认为true
                banner.isAutoPlay(true);
                //设置轮播时间
                banner.setDelayTime(1500);
                //设置指示器位置（当banner模式中有指示器时）
                banner.setIndicatorGravity(BannerConfig.CENTER);
                //banner设置方法全部调用完毕时最后调用
                banner.start();
            }
            @Override
            public void onFailure(Call<Information[]> call, Throwable t) {
                Log.e("error", t.getMessage());
                t.printStackTrace();
            }
        });
    }
    //获取最新资讯数据
    private void informationData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        MainService mainService = retrofit.create(MainService.class);
        //6, 得到Call 的方法
        Call<Information[]> call = mainService.getInformationData();
        //7, 传入接口回调 , 获取数据
       call.enqueue(new Callback<Information[]>() {
           @Override
           public void onResponse(Call<Information[]> call, Response<Information[]> response) {
               //获得数据
               Information[] information = response.body();
               List<Information> informationList=new ArrayList<>();
               Information information1;
               for (int i = 0; i < information.length; i++) {
                   information1=new Information();
                   information1.setTitle(information[i].getTitle());
                   information1.setContext(HtmlText.Html.delHTMLTag(information[i].getContext()));
                   information1.setImgUrl(information[i].getImgUrl());
                   information1.setTime(information[i].getTime());
                   informationList.add(information1);
                   Log.e("time", information[i].getTime()+"");
               }
               informationAdapter =new InformationAdapter(R.layout.item_information,informationList);
               informationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                   @Override
                   public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                       Intent intent=new Intent(MainActivity.this, ItemActivity.class);
                       intent.putExtra("title",informationList.get(position).getTitle());
                       intent.putExtra("time", Time.longToString(informationList.get(position).getTime()));
                       intent.putExtra("context", HtmlText.Html.delHTMLTag(informationList.get(position).getContext()));
                       intent.putExtra("layout","main");
                       startActivity(intent);
                   }
               });
               IrecyclerView.setAdapter(informationAdapter);
               Log.e("response", information.getClass().toString());
           }
           @Override
           public void onFailure(Call<Information[]> call, Throwable t) {
               Log.e("error", t.getMessage());
               t.printStackTrace();
           }


       });
    }
    //获取历史数据
    private void historyData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        MainService mainService = retrofit.create(MainService.class);
        //6, 得到Call 的方法
        Call<History_event[]> call = mainService.getHistory_eventData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<History_event[]>() {
            @Override
            public void onResponse(Call<History_event[]> call, Response<History_event[]> response) {
                History_event[] history_events=response.body();
                List<History_event> history_eventList=new ArrayList<>();
                History_event history_event;
                for(int i=0;i<history_events.length;i++){
                    history_event=new History_event();
                    history_event.setTitle(history_events[i].getTitle());
                    history_event.setContext(HtmlText.Html.delHTMLTag(history_events[i].getContext()));
                    history_event.setTime(history_events[i].getTime());
                    history_eventList.add(history_event);
                }
                hister_enventAdapter=new Hister_enventAdapter(R.layout.item_information,history_eventList);
                hister_enventAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent=new Intent(MainActivity.this, ItemActivity.class);
                        intent.putExtra("title",history_eventList.get(position).getTitle());
                        intent.putExtra("time",history_eventList.get(position).getTime());
                        intent.putExtra("context", HtmlText.Html.delHTMLTag(history_eventList.get(position).getContext()));
                        intent.putExtra("layout","main");
                        startActivity(intent);
                    }
                });
                HrecycleView.setAdapter(hister_enventAdapter);
                Log.e("history_events_response", history_events.getClass().toString());
            }

            @Override
            public void onFailure(Call<History_event[]> call, Throwable t) {
                Log.e("History_eventerror", t.getMessage());
                t.printStackTrace();
            }
        });
    }
    //添加导航栏数据
    private List<Navigation> addNavigation() {
        List<Navigation> data = new ArrayList<>();
        Navigation navigation;
        for (int i=0;i<5;i++){
            navigation=new Navigation();
            if (i==0){
                navigation.setId(i);
                navigation.setNavigetion("首页");
                data.add(navigation);
                continue;
            }else if (i==1){
                navigation.setId(i);
                navigation.setNavigetion("联盟介绍");
                data.add(navigation);
                continue;
            }else if (i==2){
                navigation.setId(i);
                navigation.setNavigetion("最新动态");
                data.add(navigation);
                continue;
            }else if (i==3){
                navigation.setId(i);
                navigation.setNavigetion("最新政策");
                data.add(navigation);
                continue;
            }else {
                navigation.setId(i);
                navigation.setNavigetion("互联网+");
                data.add(navigation);
                continue;
            }
        }
        return data;
    }
    //导航栏监听
    private void navigationListener(){
        navigationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TextView tv=view.findViewById(R.id.tv_navigation);
                String s=tv.getText().toString();
                if (s.equals("首页")){

                }else {
                    skip=new Skip(MainActivity.this,s);
                    intent=skip.intent();
                    startActivity(intent);
                }

                Log.e("navagationListener",tv.getText().toString());
            }
        });
    }
    //点击搜索框跳转
    private void searchIntent(){
        et_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }
    // 点击搜索图片跳转
    private void ivImageIntent(){
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }


}